<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class CxOrderService extends BaseService
{
    //案例需求订单

    /**
     * 列表
     * @param int $cateid 分类ID
     * @param array $pages【start，limit】
     * @param array $pas【orderno，xqtitle，$seller】
     */
    public function getIndex($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbCxOrder)->where('del', 0);
        if ($pas['orderno']) {
            $db = $db->where('orderno', $pas['orderno']);
        }
        if ($pas['xqtitle']) {
            $caseOnes = $this->dbConn($this->dbCaseXq)
                ->where('title', 'like', '%'.$pas['xqtitle'].'%')
                ->get();
            if (count($caseOnes)) {
                $caseIds = [];
                foreach ($caseOnes as $v) {
                    $caseIds[] = $v->id;
                }
                $db = $db->whereIn('case_id', $caseIds);
            }
        }
        if ($pas['seller']) {
            $uOnes = $this->dbConn($this->dbUser)
                ->where('uname', 'like', '%'.$pas['seller'].'%')
                ->get();
            if (count($uOnes)) {
                $uIds = [];
                foreach ($uOnes as $v) {
                    $uIds[] = $v->id;
                }
                $db = $db->whereIn('uid', $uIds);
            }
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $statuss = $this->casexqOrderStatuss;
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            //用户信息
            $uOne = (array) $this->dbConn($this->dbUser)->where('id', $v['uid'])->first();
            $uname = empty($uOne) ? '-' : $uOne['uname'];
            //案例信息
            $caseOne = (array) $this->dbConn($this->dbCaseXq)->where('id', $v['case_id'])->first();
            $case_name = empty($caseOne) ? '-' : $caseOne['title'];
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '-';
            //支付方式
            $payOne = (array) $this->dbConn($this->dbPay)
                ->where('from_type', 1)
                ->where('out_trade_no', $v['orderno'])
                ->orderBy('id', 'desc')
                ->first();
            $pay_way_name = '无';
            if (!empty($payOne)) {
                $pay_way_name = $payOne['pay_way']==1 ? '微信已支付' : '支付宝已支付';
            }
            $arys[] = [
                'id' => $v['id'],
                'orderno' => $v['orderno'],
                'uid' => $v['uid'],
                'uname' => $uname,
                'case_id' => $v['case_id'],
                'case_name' => $case_name,
                'price' => toNumber($v['price']),
                'status' => $v['status'],
                'status_name' => $status_name,
                'pay_way_name' => $pay_way_name,
                'addtime' => $v['addtime'],
                'suretime' => $v['suretime'] ? $v['suretime'] : '-',
                'finishtime' => $v['finishtime'] ? $v['finishtime'] : '-',
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 确认合作
     * @param int $id
     */
    public function setSure($id)
    {
        $res = (array) $this->dbConn($this->dbCxOrder)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '记录不存在');
        }
        if ($res['status'] > 1) {
            return $this->toback(-2, '状态错误');
        }
        $this->dbConn($this->dbCxOrder)->where('case_id', $res['case_id'])
            ->update(['status' => 2]);
        $this->dbConn($this->dbCxOrder)->where('id', $id)
            ->update(['status' => 3, 'suretime' => toDate()]);
        return $this->toback();
    }

    /**
     * 完成订单
     * @param int $id
     */
    public function setFinish($id)
    {
        $res = (array) $this->dbConn($this->dbCxOrder)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '记录不存在');
        }
        if ($res['status'] != 4) {
            return $this->toback(-2, '状态错误');
        }
        $this->dbConn($this->dbCxOrder)->where('id', $id)
            ->update(['status' => 5, 'finishtime' => toDate()]);
        $this->dbConn($this->dbCaseXq)->where('id', $res['case_id'])->update(['status' => 3]);
        return $this->toback();
    }

    /**
     * 详情
     * @param int $id
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbCxOrder)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '记录不存在');
        }
        //需求信息
        $caseOne = (array) $this->dbConn($this->dbCaseXq)->where('id', $res['case_id'])->first();
        $res['xqtitle'] = empty($caseOne) ? '-' : $caseOne['title'];
        //合作方
        $uOne = (array) $this->dbConn($this->dbUser)->where('id', $res['uid'])->first();
        $res['seller'] = empty($uOne) ? '-' : $uOne['uname'];
        $payOne = (array) $this->dbConn($this->dbPay)
            ->where('out_trade_no', $res['orderno'])
            ->where('status', 2)
            ->orderBy('id', 'desc')
            ->first();
        $res['pay_way_name'] = '无';
        if (empty($payOne)) {
            $res['pay_way_name'] = $payOne['pay_way']==1 ? '微信已支付' : '支付宝已支付';
        }
        $res['price'] = toNumber($res['price']);
        $statuss = $this->casexqOrderStatuss;
        $res['status_name'] = array_key_exists($res['status'], $statuss) ? $statuss[$res['status']] : '-';
        //订单资料
        $res['prefix'] = '/';
        $res['imgList'] = $res['videoList'] = $res['zipList'] = [];
        $files = $this->dbConn($this->dbCxoFile)
            ->where('del', 0)
            ->where('oid', $id)
            ->get();
        if (count($files)) {
            foreach ($files as $v) {
                $v = (array) $v;
                if (file_exists($v['link']) && $v['type'] == 1) {
                    $res['imgList'][] = $v['link'];
                }
                if (file_exists($v['link']) && $v['type'] == 2) {
                    $res['videoList'][] = $v['link'];
                }
                if (file_exists($v['link']) && $v['type'] == 3) {
                    $zips = explode('/', $v['link']);
                    $res['zipList'][] = [
                        'fname' => $zips[count($zips) - 1],
                        'link' => $v['link'],
                    ];
                }
            }
        }
        return $this->toback(0, '', $res);
    }

    /**
     * 添加文件
     * @param int $oid
     * @param string $fpath
     */
    public function addFile($id, $type, $fpath)
    {
        //素材库
        $fData = [
            'oid' => $id, 'type' => $type, 'link' => $fpath,
            'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbCxoFile)->insertGetId($fData);
        if (!$lastid) {
            return $this->toback(-1, '素材上传失败');
        }
        //图库
        $fData = [
            'link' => $fpath,
            'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbImg)->insertGetId($fData);
        if (!$lastid) {
            return $this->toback(-1, '图库失败');
        }
        return $this->toback();
    }

//    /**
//     * 案例删除
//     * @param int $id
//     */
//    public function del($id)
//    {
//        $res = (array) $this->dbConn($this->dbCaseXq)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return $this->toback(-2, '该分类不存在');
//        }
//        $up = $this->dbConn($this->dbCaseXq)->where('id', $id)
//            ->update(['del' => 1, 'deltime' => toDate()]);
//        if (!$up) {
//            return $this->toback(-1, '删除失败');
//        }
//        return $this->toback();
//    }
}